/* Author: Mark Mckillion
 * Student ID: B00540772
 * Date: 07/03/2012
 * Version 1.0.*/
package assignment3;

import javax.swing.*;
import java.awt.*;

@SuppressWarnings("serial")
public class DrawingPanel extends JPanel
{   /*This class will override the paintComponent method to show the graph.
     *Get and set methods will be used to update the balance and duration
     *from ControlPanel.*/
    
    //Create integers that will be used to determain points on the graph.
    private int duration, balance;
    private int d, b = 1;
    private int balArray [] = new int [100];
    private int durArray [] = new int [100];
            
    public void setBalance(int num)
    {   //This method uses an Array to set the balance points on the graph.
        balance = num; //balance will be used in an if statement to avoid errors
        /*The axis starts at 210 pixels therefore this will be the first point
         *on the graph.*/
        balArray[0] = 210;
        //Each £1000 is equilivent to 100 pixels, therefore divide by 100, plus add the 210.
        balArray[b] = -num/100 + 210;
        b++;
    }
    
    public void setDuration(long num)
    {   /*This method uses an Array to set the time passed points on the graph.
         *As the input is going to be a Long we need to Convert this to an Int.*/
        duration = (int) num;
        /*The axis starts at 240 pixels therefore this will be the first point
         *on the graph.*/
        durArray[0] = (int) 240;
        //Each week is equilivent to 9 pixels, there times by 9, plus add the 240.
        durArray[d] = (int) num * 9 + 240;
        d++;
    }
     
    @Override
    public void paintComponent(Graphics g)
    {   //Override the paintComponent to show and update the GUI.
        //Call to the super class.
        super.paintComponent(g);
        
        //Set up the different pens, colours and fonts that will be used within the graph.
        Graphics2D g2 = (Graphics2D)g;
        BasicStroke pen = new BasicStroke(1F);
        g2.setFont(new Font("SansSerif", Font.BOLD, 12));
        
        Graphics2D g1 = (Graphics2D)g;
        BasicStroke pen1 = new BasicStroke(2F);
        
        Graphics2D g3 = (Graphics2D)g;
        BasicStroke pen3 = new BasicStroke(2F);
        
        g3.setColor(Color.BLUE);
        g3.setStroke(pen3);
        
        //Draw the graph axis using DrawLine.
        g2.drawLine(240,210,710,210);
        g2.drawLine(240,50,240,250);
        
        g2.setColor(Color.BLACK);
        g2.setStroke(pen);
        
        /*DrawString methods used to add the text to the graph. This will make 
         *the interface more user friendly.*/
        g2.drawString("Elapsed Time (Weeks)",410,270);
        g2.drawString("Account Balance(£)",50,150);
        g2.drawString("16000",200,50);
        g2.drawString("-4000",200,250);
        g2.drawString("52",700,230);
        
        g1.setColor(Color.RED);
        g1.setStroke(pen1);

        /*Draw the line that will show the  balance of the model and will 
         *update every second with the balance and duration integers changing.
         *the drawPolyline method allows the user to define 2 arrays and it
         *will connect all of the points in the arrays.*/
        if(balance <= 16000 && duration <= 52) //Do this while graph will be valid.
        {   for(int i = 0; i < duration; i++)
            {   g1.drawPolyline(durArray, balArray, duration);
            }
        }  
    }
} // End of class DrawingPanel